package com.puyuba.puyuba;

import android.content.ContentResolver;
import android.content.res.Resources;
import android.net.Uri;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMUserInfo;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.delegate.EaseCustomAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseExpressionAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseFileAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseImageAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseLocationAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseTextAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseVideoAdapterDelegate;
import com.hyphenate.easeui.delegate.EaseVoiceAdapterDelegate;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.manager.EaseMessageTypeSetManager;
import com.pyb.java.commonutil.utils.StringUtil;

import com.puyuba.puyuba.utils.custommessage.ChatTxtNewAdapterDelegate;
import com.puyuba.puyuba.utils.custommessage.ShowPhoneAdapterDelegate;
import com.puyuba.puyuba.utils.custommessage.SubmitNeedAdapterDelegate;
import com.puyuba.puyuba.utils.custommessage.SystemTxtAdapterDelegate;
import com.pyb.java.cache.CacheUserInfo;

import java.util.HashMap;

public class SkinApplication extends MultiDexApplication {

    public static SkinApplication context;
    public HashMap<String, EMUserInfo> userArrayList;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        userArrayList = new HashMap<>();
        //EaseIM初始化
        if (EaseIM.getInstance().init(context, null)) {
            //在做打包混淆时，关闭debug模式，避免消耗不必要的资源
            EMClient.getInstance().setDebugMode(true);
            //EaseIM初始化成功之后再去调用注册消息监听的代码 ...
        }
        registerConversationType();
        EaseIM.getInstance().setUserProvider(username -> {
            EaseUser user = new EaseUser(username);
            String hxId = CacheUserInfo.getInstance().getsUserInfo().getUser().getHx_uid();
            if (username.equals(hxId)) {
                user.setNickname(CacheUserInfo.getInstance().getsUserInfo().getUser().getRname());
                String avatar = CacheUserInfo.getInstance().getsUserInfo().getUser().getAvatar();
                if (StringUtil.isEmpty(avatar)){
                    user.setAvatar(getMipmapToUri(R.mipmap.default_avatar));
                }else {
                    user.setAvatar(avatar);
                }
            } else {
                EMUserInfo emUserInfo = SkinApplication.getInstance().getEaseUser(username);
                //设置用户昵称
                if (emUserInfo != null)
                    user.setNickname(emUserInfo.getNickName());
                //设置头像地址
                if (emUserInfo != null)
                    user.setAvatar(emUserInfo.getAvatarUrl());
                //最后返回构建的EaseUser对象
            }

            return user;
        });
    }
    private String getMipmapToUri(int resId) {
        Resources r = getResources();
        Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + r.getResourcePackageName(resId) + "/"
                + r.getResourceTypeName(resId) + "/"
                + r.getResourceEntryName(resId));

        return uri.toString();
    }
    public void addEaseUserInfo(String username, EMUserInfo user) {
        userArrayList.put(username, user);
    }

    public EMUserInfo getEaseUser(String username) {
        return userArrayList.get(username);
    }

    /**
     * 注册对话类型
     */
    private void registerConversationType() {
        EaseMessageTypeSetManager.getInstance()
                .addMessageType(ChatTxtNewAdapterDelegate.class)//自定义交换电话消息
                .addMessageType(SystemTxtAdapterDelegate.class)//自定义系统消息
                .addMessageType(ShowPhoneAdapterDelegate.class)//自定义显示手机号
                .addMessageType(SubmitNeedAdapterDelegate.class)//自定义显示需求
                .addMessageType(EaseTextAdapterDelegate.class)//文本
                .addMessageType(EaseExpressionAdapterDelegate.class)       //自定义表情
                .addMessageType(EaseFileAdapterDelegate.class)             //文件
                .addMessageType(EaseImageAdapterDelegate.class)            //图片
                .addMessageType(EaseLocationAdapterDelegate.class)         //定位
                .addMessageType(EaseVideoAdapterDelegate.class)            //视频
                .addMessageType(EaseVoiceAdapterDelegate.class)            //声音
                .addMessageType(EaseCustomAdapterDelegate.class)           //自定义消息
                .setDefaultMessageType(EaseTextAdapterDelegate.class);       //文本
    }

    public static SkinApplication getInstance() {
        return context;
    }
}
